<?php
/**
 * Author: Traveler
 * Time  : 2021/9/4 19:17
 */

namespace app\service;


use app\model\RegionModel;
use sunshine\basic\RestService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class RegionService extends RestService
{
    /**
     * RegionService constructor.
     */
    public function __construct()
    {
        $this->model = new RegionModel();
        $this->initialize();
    }

    /**
     * 初始化
     */
    public function initialize()
    {
    }

    /**
     * 获取列表(重写)
     *
     * @param array $search 搜索参数
     * @return array
     */
    public function select(array $search = []): array
    {
        // 列表转换为树形结构
        $this->isTreeResult = true;

        // 地区列表不需要分页等操作，所以手动传入空数组
        return parent::select([]);
    }

    /**
     * 获取列表
     *
     * @param int $parentId
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getList(int $parentId = 0): array
    {
        return $this->model->where('parent_id', $parentId)->select()->toArray();
    }
}